package com.wannengbang.cloudleader.shop;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.ShopBindBean;
import com.wannengbang.cloudleader.event.BindMachinesEvent;
import com.wannengbang.cloudleader.homepage.QRActivity;
import com.wannengbang.cloudleader.shop.model.IShopModel;
import com.wannengbang.cloudleader.shop.model.ShopModelImpl;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindingMachinesActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.edit_sn)
    EditText editSn;
    @BindView(R.id.iv_sn_scan)
    ImageView ivSnScan;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ll_no_bind)
    LinearLayout llNoBind;
    @BindView(R.id.edit_mobile)
    EditText editMobile;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.tv_name1)
    TextView tvName1;
    @BindView(R.id.tv_mobile1)
    TextView tvMobile1;
    @BindView(R.id.tv_bind_sn1)
    TextView tvBindSn1;
    @BindView(R.id.tv_bind_time1)
    TextView tvBindTime1;
    @BindView(R.id.ll_bind1)
    LinearLayout llBind1;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_mobile2)
    TextView tvMobile2;
    @BindView(R.id.tv_bind_sn2)
    TextView tvBindSn2;
    @BindView(R.id.tv_bind_time2)
    TextView tvBindTime2;
    @BindView(R.id.ll_bind2)
    LinearLayout llBind2;
    @BindView(R.id.tv_add_binding)
    TextView tvAddBinding;
    private IShopModel shopModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binding_machines);
        ButterKnife.bind(this);
        shopModel = new ShopModelImpl();
        requestBindIndex();
    }

    public void requestBindIndex() {
        shopModel.requestIntegralBindIndex(new DataCallBack<ShopBindBean>() {
            @Override
            public void onSuccessful(ShopBindBean bean) {
                if (bean.getData() != null && bean.getData().size() > 0) {
                    llNoBind.setVisibility(View.GONE);
                    if (bean.getData().size() == 1) {
                        tvName1.setText(bean.getData().get(0).getMem_name());
                        tvMobile1.setText(bean.getData().get(0).getMem_mobile());
                        if (bean.getData().get(0).getIs_vip() == 1) {
                            tvBindSn1.setText(bean.getData().get(0).getNo() + "(VIP)");
                        } else {
                            tvBindSn1.setText(bean.getData().get(0).getNo());
                        }
                        String time = DateTimeUtil.format7(new Date(Long.parseLong(bean.getData().get(0).getIntegral_bind_time()) * 1000));
                        tvBindTime1.setText(time);
                        llBind1.setVisibility(View.VISIBLE);
                        llBind2.setVisibility(View.GONE);
                        tvAddBinding.setVisibility(View.VISIBLE);
                    } else if (bean.getData().size() >= 2) {
                        tvName1.setText(bean.getData().get(0).getMem_name());
                        tvMobile1.setText(bean.getData().get(0).getMem_mobile());
                        if (bean.getData().get(0).getIs_vip() == 1) {
                            tvBindSn1.setText(bean.getData().get(0).getNo() + "(VIP)");
                        } else {
                            tvBindSn1.setText(bean.getData().get(0).getNo());
                        }
                        String time1 = DateTimeUtil.format7(new Date(Long.parseLong(bean.getData().get(0).getIntegral_bind_time()) * 1000));
                        tvBindTime1.setText(time1);

                        tvName2.setText(bean.getData().get(1).getMem_name());
                        tvMobile2.setText(bean.getData().get(1).getMem_mobile());
                        if (bean.getData().get(1).getIs_vip() == 1) {
                            tvBindSn2.setText(bean.getData().get(1).getNo()+"(VIP)");
                        } else {
                            tvBindSn2.setText(bean.getData().get(1).getNo());
                        }
                        String time2 = DateTimeUtil.format7(new Date(Long.parseLong(bean.getData().get(1).getIntegral_bind_time()) * 1000));
                        tvBindTime1.setText(time2);

                        llBind1.setVisibility(View.VISIBLE);
                        llBind2.setVisibility(View.VISIBLE);
                        tvAddBinding.setVisibility(View.GONE);
                    }


                } else {
                    llNoBind.setVisibility(View.VISIBLE);
                    llBind1.setVisibility(View.GONE);
                    llBind2.setVisibility(View.GONE);
                    tvAddBinding.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void bindingMachines(String name, String mobile, String device_no) {
        shopModel.requestBindingMachines(name, mobile, device_no, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("绑定成功");
                EventBus.getDefault().post(new BindMachinesEvent());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.iv_sn_scan, R.id.tv_commit, R.id.tv_add_binding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_sn_scan:
                IntentIntegrator integrator = new IntentIntegrator(mActivity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                integrator.setOrientationLocked(false);
                integrator.setCaptureActivity(QRActivity.class);
                integrator.initiateScan();
                break;
            case R.id.tv_commit:
                String name = editName.getText().toString();
                String sn = editSn.getText().toString();
                String mobile = editMobile.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showShort("请输入您的姓名");
                    return;
                }
                if (TextUtils.isEmpty(mobile)) {
                    ToastUtil.showShort("请输入您在云商宝注册的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(sn)) {
                    ToastUtil.showShort("请输入机具编号");
                    return;
                }
                bindingMachines(name, mobile, sn);
                break;
            case R.id.tv_add_binding:
                tvAddBinding.setVisibility(View.GONE);
                llNoBind.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //扫描结果回调
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "扫描取消", Toast.LENGTH_LONG).show();
            } else {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                String device_no = scanResult.getContents();
                if (!TextUtils.isEmpty(device_no) && device_no.length() > 2 && "sn".equals(device_no.substring(0, 2))) {
                    editSn.setText(device_no.substring(2));
                    editSn.setSelection(device_no.length() - 2);
                } else {
                    editSn.setText(device_no);
                    editSn.setSelection(device_no.length());
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
